package com.macro.mall.portal.sms;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.macro.mall.common.enums.SourceTypeEnum;
import com.macro.mall.common.enums.StatusEnum;
import com.macro.mall.model.SmsMessageHistory;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20210111.models.SendStatus;
import lombok.Data;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author：郎
 * @name：SmsTencentMessage
 * @date：2023/12/11 18:59
 */
@Data
public class SmsTencentMessage implements SmsMessage {
    private final Log logger = LogFactory.getLog(SmsTencentMessage.class);
    private String secretId;
    private String secretKey;
    private String region;
    private String signName;
    private String smsSdkAppId;
    private String templateId;
    /**
     * 单位是分钟
     */
    private String expiredTime;

    @Override
    public Long getExpireTimeSecond() {
        if (StrUtil.isBlank(expiredTime)) {
            return 120L;
        } else {
            return Integer.parseInt(expiredTime) * 60L;
        }
    }

    @Override
    public SmsMessageHistory sendMessage(String phone, String verifyCode) {
        String sessionCode = UUID.randomUUID(true).toString();
        Credential credential = new Credential(secretId, secretKey);
        SmsClient smsClient = new SmsClient(credential, region);
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumberSet(new String[]{phone});
        request.setSignName(signName);
        request.setSmsSdkAppId(smsSdkAppId);
        request.setTemplateId(templateId);
        request.setTemplateParamSet(new String[]{verifyCode, expiredTime});
        request.setSessionContext(sessionCode);
        SendSmsResponse sendSmsResponse = null;
        SmsMessageHistory smsMessageHistory = new SmsMessageHistory();
        smsMessageHistory.setCode(verifyCode);
        smsMessageHistory.setTemplateId(templateId);
        smsMessageHistory.setPhone(phone);
        smsMessageHistory.setExpiredMinute(expiredTime);
        smsMessageHistory.setSourceType(SourceTypeEnum.TENCENT.getCode());
        smsMessageHistory.setSessionCode(sessionCode);
        try {
            sendSmsResponse = (SendSmsResponse) smsClient.retry(request, 3);
        } catch (TencentCloudSDKException e) {
            logger.error("腾讯云短信发送短信失败：" + e.getMessage(), e);
        }
        if (!ObjectUtil.isNull(sendSmsResponse) && sendSmsResponse.getSendStatusSet().length > 0) {
            SendStatus sendStatusSet = sendSmsResponse.getSendStatusSet()[0];
            smsMessageHistory.setSerialNo(sendStatusSet.getSerialNo());
            smsMessageHistory.setStatus(StatusEnum.SUCCESS.getCode());
        } else {
            smsMessageHistory.setStatus(StatusEnum.FAILURE.getCode());
        }
        return smsMessageHistory;
    }

    @Override
    public Object getResponseStatus() {
        return null;
    }
}
